package cn.bdqn.servlet;

import cn.bdqn.bean.Topic;
import cn.bdqn.service.TopicService;
import cn.bdqn.service.impl.TopicServiceImpl;
import cn.bdqn.util.PageUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@WebServlet(name = "TopicServlet",urlPatterns = "/topic")
public class TopicServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TopicService topicService=new TopicServiceImpl();
        String opr=request.getParameter("opr");

        if("del".equals(opr)){
            int id =Integer.parseInt(request.getParameter("tid"));
            int result=topicService.del(id);
            if(result>0){
                System.out.println(request.getContextPath()+"/topic?opr=list");
                response.sendRedirect(request.getContextPath()+"/topic?opr=list");
            }
        }else if("upd".equals(opr)){
            int id =Integer.parseInt(request.getParameter("tid"));
            Topic topic=topicService.getOne(id);
            request.setAttribute("topic",topic);
            request.getRequestDispatcher("/newspages/topic_modify.jsp").forward(request,response);
        }else if("list".equals(opr)){
            //分页查询
            //从页面获取当前页码，如果为空，则赋值为1
            String pageStr=request.getParameter("pageNo");
            Integer pageNo=0;
            if(null==pageStr){
                pageNo=1;
            }else{
                pageNo=Integer.parseInt(pageStr);
            }
                Map<String,Object> map=topicService.getPage(pageNo);
                PageUtil pageUtil=(PageUtil) map.get("page");
                List<Topic> list=( List<Topic>)map.get("list");
                /*request.setAttribute("list",map.get("list"));
                request.setAttribute("page",map.get("page"));*/

               /* request.getRequestDispatcher("/newspages/topic_list.jsp").forward(request,response);*/
                String mapJSON= JSON.toJSONString(map, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty);
                response.setContentType("text/html;charset=utf-8");
                response.setCharacterEncoding("utf-8");
                PrintWriter out=response.getWriter();
                out.print(mapJSON);
                out.flush();
                out.close();
            }else if("doUpd".equals(opr)){
            //request.setCharacterEncoding("utf-8");
            String tname=request.getParameter("tname");
            Integer tid=Integer.parseInt(request.getParameter("tid"));
            int result=topicService.updTopic(new Topic(tid,tname));
            if(result>0){
                response.sendRedirect(request.getContextPath()+"/topic?opr=list");
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
